/**
 * Created by XiMing on 2015/6/21.
 */
var co = require('co'),
    request = require('request'),
    schedule = require("node-schedule"),
    cheerio  = require('cheerio'),
    db = require('./db');
var getinfo = function() {
    request("http://ssdut.dlut.edu.cn/index/bkstz.htm", function (error, response, body) {
        if (!error && response.statusCode == 200) {
            $ = cheerio.load(body);
            var res = $('.c_hzjl_list1 li');
            var len = res.length;
            for (var i = 0; i < len; i++) {
                (function(i){
                    var $$ = cheerio.load(res[i]);
                    var title = $$('a').text();
                    var link = $$('a').attr('href').substr(2);
                    var data = $$('span').text();
                    db.zhouzhi.find({where:{link:link}}).then(function(project){
                        if(!project){
                            db.zhouzhi.create({title:title,link:link,data:data}).then(function(p){});
                        }
                        console.log(project);
                    })
                }(i));
            }
        }
    });
};
var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=1; i<60; i++){
    if(i%2==0){
        times.push(i);
    }
}
rule.minute = times;

var j = schedule.scheduleJob(rule, function(){
    getinfo();
});
